package com.zuipin.service.impl;

import java.math.BigDecimal;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.zuipin.entity.CashBackRecord;
import com.zuipin.entity.MemberAccount;
import com.zuipin.enums.CashBackRecordType;
import com.zuipin.framework.emuns.ErrorCode;
import com.zuipin.framework.exception.CustomException;
import com.zuipin.mapper.CashBackRecordMapper;
import com.zuipin.service.ICashBackRecordService;
import com.zuipin.service.IMemberAccountService;
import com.zuipin.util.ArithUtil;
import com.zuipin.util.ConstantsUtil;
import com.zuipin.util.DateUtils;

/**
 * @ClassName: CashBackRecordServiceImpl
 * @author wuyicheng
 * @time 2017年3月27日下午4:01:17
 */
@Service
public class CashBackRecordServiceImpl implements ICashBackRecordService {
	@Resource
	private CashBackRecordMapper	cashBackRecordMapper;
	@Resource
	private IMemberAccountService	memberAccountService;
	
	/**
	 * @Title: findById
	 * @author: wuyicheng
	 * @date: 2017年3月27日下午5:01:23
	 * @param id
	 * @return
	 * @throws
	 */
	@Override
	public CashBackRecord findById(Long id) {
		return cashBackRecordMapper.findById(id);
	}
	
	/**
	 * 根据会员ID查询返现记录
	 * 
	 * @Title: findList
	 * @author: wuyicheng
	 * @date: 2017年3月27日下午5:01:23
	 * @param memberId
	 * @return
	 * @throws
	 */
	@Override
	public List<CashBackRecord> findListByMemberId(Long memberId) {
		//return cashBackRecordMapper.findListByMemberId(memberId);
		return null;
	}
	
	/**
	 * @Title: insert
	 * @author: wuyicheng
	 * @date: 2017年3月27日下午5:01:23
	 * @param cashBackRecord
	 * @throws
	 */
	@Override
	public void insert(CashBackRecord cashBackRecord) {
		cashBackRecord.setDelFlag(ConstantsUtil.DEL_FLAG_NO_0);
		cashBackRecord.setCreateTime(DateUtils.getCurrentDateTime());
		cashBackRecord.setUpdateTime(DateUtils.getCurrentDateTime());
		cashBackRecordMapper.insert(cashBackRecord);
		
	}
	
	/**
	 * @Title: update
	 * @author: wuyicheng
	 * @date: 2017年3月27日下午5:01:23
	 * @param cashBackRecord
	 * @throws
	 */
	@Override
	public void update(CashBackRecord cashBackRecord) {
		cashBackRecord.setUpdateTime(DateUtils.getCurrentDateTime());
		cashBackRecordMapper.update(cashBackRecord);
		
	}
	
	/**
	 * @Title: deleteById
	 * @author: wuyicheng
	 * @date: 2017年3月27日下午5:01:23
	 * @param id
	 * @throws
	 */
	@Override
	public void deleteById(Long id) {
		CashBackRecord cashBackRecord = new CashBackRecord();
		cashBackRecord.setId(id);
		cashBackRecord.setDelFlag(ConstantsUtil.DEL_FLAG_YES_1);
		update(cashBackRecord);
	}
	
	/**
	 * @throws CustomException 
	 * 更新会员返现余额
	 * 
	 * @Title: updateCashBackAmount 
	 * @author: wuyicheng
	 * @date: 2017年4月25日下午8:47:09 
	 * @param memberId 会员ID
	 * @param sysId 系统ID
	 * @param createId 创建者ID
	 * @param updateId 修改者ID
	 * @param alterPoint 操作金额
	 * @param cashBackRecordType 返现类型
	 * @param remark 备注
	 * @throws 
	 */
	@Override
	public void updateCashBackAmount(Long memberId, Long sysId, Long createId, Long updateId,
			BigDecimal amount, CashBackRecordType cashBackRecordType, String remark)
			throws CustomException {
		// TODO Auto-generated method stub
		MemberAccount memberAccount = memberAccountService.findByMemberIdAndSysId(memberId, sysId);
		if (memberAccount != null) {
			/*boolean isBinding = memberAccountService
					.isBindingMobilePhone(memberAccount.getMemberId());
			//账号未绑定手机号，不能操作
			if (!isBinding) {
				throw new CustomException(ErrorCode.DB_EXCEPTION.getCode(),
						"Account not binding mobile phone number, can't operation");
			}*/
			CashBackRecord cashBackRecord = new CashBackRecord();
			cashBackRecord.setBeforeAmount(memberAccount.getTotalRefundAmount());
			BigDecimal totalRefundAmount = memberAccount.getTotalRefundAmount();
			//			if (CashBackRecordType.DEDUCTION.equals(cashBackRecordType)) {
			//				totalRefundAmount = ArithUtil.sub(totalRefundAmount, amount, 2);
			//				cashBackRecord.setAmount(ArithUtil.sub(amount, new BigDecimal(-1), 2));
			//			} else {
			//				totalRefundAmount = ArithUtil.add(totalRefundAmount, amount, 2);
			//				cashBackRecord.setAmount(amount);
			//			}
			totalRefundAmount = ArithUtil.add(totalRefundAmount, amount, 2);
			//使用返现余额大于当前可用余额
			//0等于，1大于，-1小于
			int totalRefundAmountFlag = ArithUtil.compareTo(totalRefundAmount, new BigDecimal(0));
			int amountFlag = ArithUtil.compareTo(amount, new BigDecimal(0));
			//减少操作，且减少后余额小于0不能操作
			if (totalRefundAmountFlag == -1 && amountFlag == -1) {
				throw new CustomException(ErrorCode.DB_EXCEPTION.getCode(),
						"Using cashback balance is greater than the current available balance");
			}
			cashBackRecord.setAmount(amount);
			cashBackRecord.setRecordType(cashBackRecordType);
			cashBackRecord.setAfterAmount(totalRefundAmount);
			cashBackRecord.setRemark(remark);
			cashBackRecord.setCreateId(createId);
			cashBackRecord.setUpdateId(updateId);
			cashBackRecord.setMemberId(memberId);
			cashBackRecord.setSysId(sysId);
			insert(cashBackRecord);
			//更新账号
			MemberAccount ma = new MemberAccount();
			ma.setId(memberAccount.getId());
			ma.setTotalRefundAmount(totalRefundAmount);
			memberAccountService.update(ma);
		} else {
			throw new CustomException(ErrorCode.DB_EXCEPTION.getCode(),
					"memberAccount is not exist");
		}
	}
	
}
